//
//  QJMineAddressCell.m
//  QuanJieShopProject
//
//  Created by 刘中义 on 2018/2/19.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "QJMineAddressCell.h"
#import "QJMineEditAddressVC.h"
@implementation QJMineAddressCell
-(void)cellUpdateWithModel:(MineAddressModel *)model{
    _model = model;
    if (model) {
        NSString *nameStr = [NSString stringWithoutNilFormart:model.link_name];
        NSString *telStr = [NSString stringWithoutNilFormart:model.tel];
        NSString *provineStr = [NSString stringWithoutNilFormart:model.province];
        NSString *cityStr = [NSString stringWithoutNilFormart:model.city];
        NSString *regionStr = [NSString stringWithoutNilFormart:model.region];
        NSString *detailStr = [NSString stringWithoutNilFormart:model.detail_address];
        NSString *defaultStr = [NSString stringWithoutNilFormart:model.is_default];
        self.nameLabel.text = [NSString stringWithFormat:@"%@ %@",nameStr,telStr];
        self.addressLabel.text = [NSString stringWithFormat:@"%@ %@ %@ %@",provineStr,cityStr,regionStr,detailStr];
       
        if ([defaultStr isEqualToString:@"1"]) {
            
            self.defaultBtn.selected = YES;
            
        }else{
            
            self.defaultBtn.selected = NO;
        }
    }
}
- (void)awakeFromNib {
    [super awakeFromNib];
     self.defaultLabel.text = @"默认地址";
    [self.defaultBtn setImage:[UIImage imageNamed:@"Snoselect"] forState:UIControlStateNormal];
    [self.defaultBtn setImage:[UIImage imageNamed:@"Sselect"] forState:UIControlStateSelected];
}
-(void)setFrame:(CGRect)frame{
    
    // 更改顶部间距、每个cell之间的间距
    frame.origin.y += 10;
    frame.size.height -= 10;
    [super setFrame:frame];
}
- (IBAction)defautlBtnAction:(UIButton *)sender {
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:_model.address_id forKey:@"id"];
    [WWNetRequest fetchSetDefaultAddressWithParameters:dict success:^(id response) {
        [[NSNotificationCenter defaultCenter]postNotificationName:@"defaultAddressNotification" object:nil];
    } failure:^(NSDictionary *error) {
        [MBPHUD showAlertWithTitle:@"设置失败"];
    }];
   
    
}
- (IBAction)delBtnAction:(UIButton *)sender {
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:_model.address_id forKey:@"id"];
    
    [WWNetRequest fetchDeleteAddressWithParameters:dict success:^(id response) {
        
        [[NSNotificationCenter defaultCenter]postNotificationName:@"delAddressNotification" object:nil];
    } failure:^(NSDictionary *error) {
        
        [MBPHUD showAlertWithTitle:@"删除失败"];
    }];
    
}
- (IBAction)editBtnAction:(UIButton *)sender {
    
    QJMineEditAddressVC *VC = [[QJMineEditAddressVC alloc]init];
    VC.model = _model;
    UIViewController *vc = [self currentViewController];
    [vc.navigationController pushViewController:VC animated:YES];
}

//当前视图
-(UIViewController *)currentViewController{
    UIViewController *vc;
    for (UIView* next = [self superview]; next; next = next.superview) {
        UIResponder* nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[objc_getClass("UIViewController") class]] ) {
            vc=(UIViewController*)nextResponder;
            
            return vc;
        }
    }
    return vc;
}

@end
